package by.grsu.verkhovodko.page;



import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;

import by.grsu.verkhovodko.app.VerkhovodkoWebApplication;
import by.grsu.verkhovodko.entity.Passenger;
import by.grsu.verkhovodko.dao.impl.PassengerDao;

public class Authorization extends WebPage{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1087354574704096953L;

	@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
	public Authorization() {
		super();
		final Form form = new Form("form");
		add(form);

		final TextField login = new TextField("login", new Model());
		final TextField password = new TextField("password", new Model());

		form.add(login);
		form.add(password);

		form.add(new SubmitLink("logIn") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				
					PassengerDao passengerDao = new PassengerDao(
							VerkhovodkoWebApplication.DATA_XML_FOLDER);
					
					Passenger passenger = passengerDao.searchByLogin(login.getValue());
					if (passenger != null && passenger.getPassword().equals(password.getValue())) {
						setResponsePage(new UserPage(passenger)); 
						
					} 
					else {		

							setResponsePage(Authorization.class);						
					}
				 
			}
		});

		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				setResponsePage(StartPage.class);
			}
		});







	}

}
